package BlackJack.View
{
	import BlackJack.Model.Events.LobbyEvent;
	import BlackJack.Model.Events.TableListEvent;
	import BlackJack.Model.VO.Player;
	import BlackJack.Model.VO.TableData;
	import caurina.transitions.Tweener;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Dictionary;

	public class Lobby extends Sprite
	{
		/**	顯示物件 */
		private var _display:MovieClip;
		/**	key為LobbyEvent的Events...value為各按鈕 */
		private var btnDict:Dictionary      = new Dictionary();
		/**	keyDict 為 btnDict 的Reverse */
		private var keyDict:Dictionary      = new Dictionary();
		private var infoDict:Dictionary     = new Dictionary();
		private var modelDict:Dictionary    = new Dictionary();
		private var typeDict:Dictionary     = new Dictionary();
		/**	金幣顯示(& 頁數) */
		private var pointPlayer:MovieClip;
		private var _point:int              = -1;
		private var pointBonus:MovieClip;
		private var _bonus:int              = -1;
		private var pointPage:MovieClip;
		private var _page:int               = -1;
		/**/
		public static const tableNum:int    = 5;
		public static const MaxPoint:String = 'MaxPoint';
		public static const MinPoint:String = 'MinPoint';
		public static const Index:String    = 'Index';
		public static const Player:String   = 'Player';
		public static const Infos:Array     = [MaxPoint, MinPoint, Index, Player];
		/**/
		public static const tableTypes:int  = 3;
		public static const pageNumber:int  = 20;
		private var info:MovieClip;
		private var _type:int               = 0;
		/**/
		private var data:Vector.<TableData> = new Vector.<TableData>(100);
		public var md5:Vector.<uint>        = new Vector.<uint>(20);

		public function Lobby()
		{
			for (var i:int = 0; i < 100; i++)
			{
				data[i] = new TableData();
			}
		}

		public function get display():MovieClip
		{
			return _display;
		}

		public function set display(value:MovieClip):void
		{
			_display = value;
			addChild(value);
			/**	玩家金幣及獎金顯示 */
			pointPlayer = value.Menu.PointPlayer;
			pointBonus = value.PointBonus;
			pointPage = value.Menu.PointPage;
			var btn:SimpleButton;
			var s:String;
			for each (s in LobbyEvent.Events)
			{
				btn = value.Menu['btn' + s];
				btnDict[s] = btn;
				btn.addEventListener(MouseEvent.CLICK, btnHandler);
				btn.focusRect = false;
				keyDict[btn] = s;
			}
			/**/
			var i:int;
			var j:int;
			var k:int;
			var tf:TextField;
			info = value.Info;
			for (i = 0; i < tableNum; i++)
			{
				infoDict[i] = value['Info' + i];
				modelDict[i] = value['Model' + i];
				for each (s in Infos)
				{
					tf = infoDict[i][s];
					tf.autoSize = TextFieldAutoSize.LEFT;
					tf.defaultTextFormat = tf.getTextFormat();
				}
			}
			for (i = 0; i < 3; i++)
			{
				typeDict[i] = value['Type' + i];
				MovieClip(typeDict[i]).visible = false;
				for (j = 0; j < 5; j++)
				{
					for (k = -1; k < 5; k++)
					{
						btn = Seat(i, j, k);
						if (btn != null)
						{
							if (btn.name.indexOf('Seat') > -1)
							{
								btn.addEventListener(MouseEvent.CLICK, seatHandler);
							}
							btn.focusRect = false;
						}
					}
				}
			}
			type = 0;
			Page = 0;
			Bonus = 0;
			//	Initial Setting
			Btn(LobbyEvent.HighQuality).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			Btn(LobbyEvent.NormalScreen).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			Btn(LobbyEvent.SoundOff).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}

		public function get type():int
		{
			return _type;
		}

		public function set type(value:int):void
		{
			Type(_type).visible = false;
			Type(value).visible = true;
			_type = value;
		}

		public function get Bonus():int
		{
			return _bonus;
		}

		public function set Bonus(value:int):void
		{
			_bonus = value;
			BlackJackMain.updateValue(pointBonus, value);
		}

		public function delayBonus(value:int):void
		{
			Tweener.addTween(this, {Bonus: value, time: 3});
		}

		public function get Point():int
		{
			return _point;
		}

		public function set Point(value:int):void
		{
			_point = value;
			BlackJackMain.updateValue(pointPlayer, value);
		}

		public function delayPoint(value:int):void
		{
			Tweener.addTween(this, {Point: value, time: 3});
		}

		public function get Page():int
		{
			return _page;
		}

		public function set Page(value:int):void
		{
			if (_page == value)
			{
				return;
			}
			_page = value;
			BlackJackMain.updateValue(pointPage, value);
			switch (_page)
			{
				case 0:
				case 1:
				{
					type = 0;
					break;
				}
				case 18:
				case 19:
				{
					type = 2;
					break;
				}
				default:
				{
					type = 1;
					break;
				}
			}
		}

		private function btnHandler(event:MouseEvent):void
		{
			var btn:SimpleButton  = event.currentTarget as SimpleButton;
			var type:String       = keyDict[btn];
			var nEvent:LobbyEvent = new LobbyEvent(type);
			switch (type)
			{
				case LobbyEvent.NextPage:
				{
					Page = (Page + 1) % pageNumber;
					break;
				}
				case LobbyEvent.LastPage:
				{
					Page = (Page - 1 + pageNumber) % pageNumber;
					break;
				}
			}
			nEvent.page = Page;
			dispatchEvent(nEvent);
		}

		private function seatHandler(event:MouseEvent):void
		{
			var btn:SimpleButton             = event.currentTarget as SimpleButton;
			var table:DisplayObjectContainer = btn.parent;
			var pos:int                      = int(btn.name.replace('Seat', ''));
			var index:int                    = int(table.name.replace('Table', ''));
			var nEvent:LobbyEvent            = new LobbyEvent(LobbyEvent.EnterTable);
			nEvent.index = data[Page * 5 + index].Index;
			nEvent.pos = pos;
			if (type == 0)
			{
				nEvent.tableType = index > 1 ? 1 : 0;
			}
			else
			{
				nEvent.tableType = type;
			}
			dispatchEvent(nEvent);
		}

		public function lobbyHandler(event:LobbyEvent):void
		{
			switch (event.type)
			{
				case LobbyEvent.Auto:
				case LobbyEvent.Charge:
				case LobbyEvent.Exit:
					break;
				case LobbyEvent.NextPage:
				case LobbyEvent.LastPage:
				{
					update();
					break;
				}
				case LobbyEvent.FullScreen:
				{
					Btn(LobbyEvent.FullScreen).visible = false;
					Btn(LobbyEvent.NormalScreen).visible = true;
					break;
				}
				case LobbyEvent.NormalScreen:
				{
					Btn(LobbyEvent.FullScreen).visible = true;
					Btn(LobbyEvent.NormalScreen).visible = false;
					break;
				}
				case LobbyEvent.HighPerformance:
				{
					Btn(LobbyEvent.HighQuality).visible = true;
					Btn(LobbyEvent.HighPerformance).visible = false;
					break;
				}
				case LobbyEvent.HighQuality:
				{
					Btn(LobbyEvent.HighQuality).visible = false;
					Btn(LobbyEvent.HighPerformance).visible = true;
					break;
				}
				case LobbyEvent.SoundOff:
				{
					Btn(LobbyEvent.SoundOff).visible = false;
					Btn(LobbyEvent.SoundOn).visible = true;
					break;
				}
				case LobbyEvent.SoundOn:
				{
					Btn(LobbyEvent.SoundOn).visible = false;
					Btn(LobbyEvent.SoundOff).visible = true;
					break;
				}
			}
		}

		public function setTableList(event:TableListEvent):void
		{
			for (var i:int = 0; i < 5; i++)
			{
				var item:TableData = event.data[i];
				data[item.Index] = item;
			}
			update();
		}

		private function update():void
		{
			for (var i:int = 0; i < tableNum; i++)
			{
				var item:TableData = data[Page * 5 + i];
				Textfield(i, MaxPoint).text = TableData.NumberToString(item.MaxPoint);
				Textfield(i, MinPoint).text = TableData.NumberToString(item.MinPoint);
				Textfield(i, Index).text = item.Index.toString();
				Textfield(i, Player).text = item.PlayerCount.toString() + '/' + item.PlayerMax.toString();
				for (var j:int = 0; j < 5; j++)
				{
					var icon:int         = item.ModelNo[j];
					var btn:SimpleButton = Seat(type, i, j);
					if (btn != null)
					{
						btn.mouseEnabled = icon > 0 ? false : true;
						Model(i, j).gotoAndStop('avatar' + icon);
					}
					else
					{
						Model(i, j).gotoAndStop(1);
					}
				}
			}
		}

		private function Btn(value:String):SimpleButton
		{
			return btnDict[value];
		}

		private function Textfield(index:int, value:String):TextField
		{
			return infoDict[index][value];
		}

		private function Model(index:int, pos:int):MovieClip
		{
			return modelDict[index]['Icon' + pos];
		}

		private function Type(index:int):MovieClip
		{
			return typeDict[index];
		}

		private function Table(type:int, index:int):MovieClip
		{
			return Type(type)['Table' + index];
		}

		private function Seat(type:int, index:int, pos:int):SimpleButton
		{
			if (pos == -1)
			{
				return Table(type, index)['Table'];
			}
			else
			{
				return Table(type, index)['Seat' + pos];
			}
		}
	}
}
